﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.BreedCommand;
using B.S.Traceability.Write.API.Applicantion.Command.EnterCommand;
using B.S.Traceability.Write.API.Applicantion.Command.GoodsCommand;
using B.S.Traceability.Write.API.Applicantion.Command.HealthCommand;
using B.S.Traceability.Write.API.Applicantion.Command.PhoneCommand;
using B.S.Traceability.Write.API.Applicantion.Command.SlaughterInfoCommand;
using B.S.Traceability.Write.API.Applicantion.Command.SupplierCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace B.S.Traceability.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManageController : ControllerBase
    {
        private readonly IMediator mediator;

        public ManageController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {
            try
            {
                var res = "wwwroot/tu/" + file.FileName;
                using (FileStream fs=new FileStream(res,FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7221/tu/" + file.FileName);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost] 
        public Task<APIResult<int>> AddGood(AddGoodsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 更新商品
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public Task<APIResult<int>> UpdateGood(UpdateGoodsCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<object>> UpdateGoodsRanhes(DelGoodsCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddBreedInfo(AddBreedCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddHealth(AddHealthCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 更新保健信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateHealth(UpdateHealthCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteHealth(DelHealthCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 添加公司信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddEnter(AddEnterCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 修改公司信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateEnter(UpdateCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 添加屠宰信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddSlaughter(AddSlaughterInfoCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 添加图片信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddPhone(AddPhoneCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 删除图片信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> DelPhone(DelPhoneCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 添加供应商信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddSupplier(AddSupplierCommand request)
        {
            return await mediator.Send(request);
        }

        /// <summary>
        /// 删除供应商信息
        /// </summary>
        /// <param name="request">实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DelSupplier(DeleteSupplierCommand request)
        {
            return await mediator.Send(request);
        }
    }
}
